/**
 *  Copyright(C) 2012-2013 Suntec(Shanghai) Software Co., Ltd.
 *  All Right Reserved.
 */
package com.cugbmao.luence;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Descriptions
 * 
 * @version 2013年5月20日
 * @author Suntec
 * @since JDK1.6
 * 
 */
public class Indexer {
	private final IndexWriter writer;

	public Indexer( Directory indexDir ) throws IOException {
// Directory dir = FSDirectory.open( new File( indexDir ) );
		Analyzer analyzer = new StandardAnalyzer( Version.LUCENE_43 );
// Analyzer analyzer = new IKAnalyzer( true );
		IndexWriterConfig iwc = new IndexWriterConfig( Version.LUCENE_43, analyzer );
		iwc.setOpenMode( OpenMode.CREATE );
		writer = new IndexWriter( indexDir, iwc );
	}

	/**
	 * @param args
	 */
	public static void main( String[] args ) throws Exception {
		if ( args.length != 2 ) {
			throw new Exception( "Usage: java " + Indexer.class.getName() + " <index dir> <data dir>" );
		}
		File indexDir = new File( args[0] );
		File dataDir = new File( args[1] );
		Indexer indexer = new Indexer( FSDirectory.open( indexDir ) );
		long start = new Date().getTime();
		int numIndexed = 0;
		try {
			numIndexed = indexer.index( indexDir, dataDir );
		}
		finally {
			indexer.close();
		}
		long end = new Date().getTime();

		System.out.println( "Indexing " + numIndexed + " files took " + ( end - start ) + " milliseconds" );
	}

	public void close() throws IOException {
		writer.close(); // 4
	}

	public int index( File indexDir, File dataDir ) throws IOException {
		if ( !dataDir.exists() || !dataDir.isDirectory() ) {
			throw new IOException( dataDir + " does not exist or is not a directory" );
		}

		indexDirectory( writer, dataDir );

		return writer.numDocs();
	}

	private void indexDirectory( IndexWriter writer, File dir ) throws IOException {
		File[] files = dir.listFiles();
		for ( int i = 0; i < files.length; i++ ) {
			File file = files[i];
			if ( file.isDirectory() ) {
				indexDirectory( writer, file );
			}
			else {
				indexFile( writer, file );
			}
		}
	}

	private void indexFile( IndexWriter writer, File file ) throws IOException {
		if ( file.isHidden() || !file.exists() || !file.canRead() ) {
			return;
		}

		System.out.println( "Indexing " + file.getCanonicalPath() );

		Document doc = new Document();
		Field fieldName = new StringField( "filename", file.getName(), Field.Store.YES );
		Field fieldContent = new TextField( "contents", new FileReader( file ) );
		Field fieldPath = new StringField( "fullpath", file.getCanonicalPath(), Field.Store.YES );

		doc.add( fieldName );
		doc.add( fieldContent );
		doc.add( fieldPath );
		writer.addDocument( doc );
	}
}
